package com.cxg.allsys.memsys.service.impl;


import com.cxg.allsys.memsys.mapper.MemberMapper;
import com.cxg.allsys.memsys.pojo.Member;
import com.cxg.allsys.memsys.service.IMemberService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@Data
public class MemberService implements IMemberService {

    Logger logger = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberMapper memberMapper;


    //    会员注册
    @Override
    public boolean save(Member member) {
        log.info("service实现类");
        try {
            logger.debug("service对象正常注入获取");
            memberMapper.insert(member);

            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return false;
        }
    }

//    会员登录
    @Override
    public Member qure(String usname, String password) {
        log.info("service会员登录==实现类");
        log.info(usname+password);
        Member u1 = memberMapper.qure(usname,password); ;
        log.info("查询数据库后的返回值"+String.valueOf(u1));
        if(u1==null){
            return null;
        }else{
            if(u1.getPassword().equals(password)){
                return u1;
            }else{
                return null;
            }
        }
    }

//    根据ID查找会员信息
    @Override
    public List<Member> getById(Integer id) {
        return memberMapper.getSelectById(id);
    }

    //会员中心中修改会员信息:
    @Override
    public boolean updateMember(Member member) {
        return memberMapper.updateMember(member);
    }

    //会员中心中重置秘密
    @Override
    public String ResetMemberPassword(Integer id, String oldPassWord, String newPassWord) {
        List<Member> list=memberMapper.getSelectById(id);
//        log.info(String.valueOf(list.get(0).getPassword()));
        if(list.get(0).getPassword().equals(oldPassWord)){
            log.info("旧密码正确");
            log.info("开始进行修改密码操作");
            if (memberMapper.updatepassword(id,newPassWord))
               return "修改成功";
            else return "修改失败";
        }else {
            return "旧密码输入错误";
        }

    }

    //会员注销
    @Override
    public boolean del(Integer id) {
        try {

            return memberMapper.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
